﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Security.Cryptography;
using System.Net;
using System.IO;

namespace XProject.DataVisit.BLL
{
    /// <summary>
    /// 公共方法的集合类
    /// </summary>
    public class PublicFn
    {
        #region 获取本周的天数
        public Dictionary<string,object> GetCurrentWeek()
        {
            var currentday = DateTime.Now.DayOfWeek;
            var firstday = new DateTime(DateTime.Now.AddDays(0 - ((int)currentday - 1)).Year, 
                DateTime.Now.AddDays(0 - ((int)currentday - 1)).Month, 
                DateTime.Now.AddDays(0 - ((int)currentday - 1)).Day,0,0,0);
            var endday = new DateTime(DateTime.Now.AddDays(7 - (int)currentday).Year, DateTime.Now.AddDays(7 - (int)currentday).Month, DateTime.Now.AddDays(7 - (int)currentday).Day,23,59,59);
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("FTime", firstday);
            param.Add("ETime", endday);
            return param;
        }
        #endregion

        #region 获取本月的天数
        public Dictionary<string, object> GetCurrentMonth()
        {
            var currentday = DateTime.Now;
            var firstday = new DateTime(currentday.Year, currentday.Month, 1,0,0,0);
            var endday = new DateTime(currentday.Year, currentday.Month, DateTime.DaysInMonth(currentday.Year, currentday.Month),23,59,59);
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("FTime", firstday);
            param.Add("ETime", endday);
            return param;
        }
        #endregion

        #region 获取本年的天数
        public Dictionary<string, object> GetCurrentYear()
        {
            var currentday = DateTime.Now;
            var firstday = new DateTime(currentday.Year, 1, 1,0,0,0);
            var endday = new DateTime(currentday.Year, 12, 31,23,59,59);
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("FTime", firstday);
            param.Add("ETime", endday);
            return param;
        }
        #endregion

        #region 获取当天时间
        public Dictionary<string, object> GetCurrentDay()
        {
            var currentday = DateTime.Now;
            var firstday = new DateTime(currentday.Year, currentday.Month, currentday.Day,0,0,0);
            var endday = new DateTime(currentday.Year, currentday.Month, currentday.Day, 23, 59, 59);
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("FTime", firstday);
            param.Add("ETime", endday);
            return param;
        }
        #endregion

        #region 遍历HashTable
        public static IDictionary<string,object> GetHashValue(Hashtable data)
        {
            IDictionary<string, object> param = new Dictionary<string, object>();
            foreach (DictionaryEntry d in (Hashtable)data)
            {
                param.Add(d.Key.ToString(),d.Value!=null?d.Value.ToString():"0");
            }
            return param;
        }
        #endregion

        #region 32位MD5加密
        /// <summary>
        /// 32位MD5加密
        /// </summary>
        /// <param name="encryptStr"></param>
        /// <returns></returns>
        public static string getMD5(string encryptStr)
        {
            System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(encryptStr));
            string strResult = BitConverter.ToString(bytResult);
            strResult = strResult.Replace("-", "");
            return strResult;
        }
        #endregion

        #region 16位MD5加密
        /// <summary>
        /// 16位MD5加密
        /// </summary>
        /// <param name="encryptStr"></param>
        /// <returns></returns>
        public static string Get16MD5(string encryptStr)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(encryptStr)), 4, 8);
            t2 = t2.Replace("-", "");
            return t2;

        }
        #endregion

        #region 获取HTML页面代码
        public static string GetHTMLPage(string path)
        {
            string s = "";
            byte[] buf = new byte[38192];

            //实例化HttpWebRequest对象，使用WebRequest类的静态方法Create()
            //该方法的参数是要请求页面的URL地址，由于Create()方法返回的是WebRequest类型的
            //我们必须对它进行造型（即类型转换）成HttpWebRequest类型，再赋给request变量
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);

            //一旦建立了HttpWebRequest对象，就可以使用它的GetResponse()方法来返回一个WebResponse对象
            //然后再造型成HttpWebResponse对象赋给response变量。
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            //使用response对象的GetResponseStream()方法来得到响应的文本流了，
            Stream resStream = response.GetResponseStream();

            //最后用Stream对象的Read()方法把返回的响应信息放到字节数组中，
            //Read()有3个参数，分别是：要放入的字节数组，字节数组的开始位置，字节数组的长度。
            int count = resStream.Read(buf, 0, buf.Length);

            //最后把字节转换成字符串，
            s = Encoding.Default.GetString(buf, 0, count);

            //关闭流
            resStream.Close();
            return s;

        }
        #endregion

        #region 删除文件夹
        public static bool DelFolder(string path)
        {
            if (Directory.Exists(path))
            {
                foreach (string d in Directory.GetFileSystemEntries(path))
                {
                    if (File.Exists(d))
                    {
                        File.Delete(d);
                    }
                    else
                    {
                        DelFolder(d);
                    }
                }
                Directory.Delete(path);
                return true;
            }
            return false;
        }
        #endregion
    }

}
